import * as types from '../action-typess';
export default function reducer(state={
  banner: [],
  tabList: [],
  allTabList: [],
  todayHotProducts: [],
  fetched: false,
  page : 1,
  hotProducts: [],
  detail: {
    small_images: [],
    items_content: []
  },
  childrenList: [],
  officeMessage: [],
  recommendGoodsList: [],
  prize: []
}, action) {
  //判断类型做出对应state的改变
  switch (action.type){
    // 首页
    case types.FETCH_HOME: {
      return {...state, fetching: true}
    }
    case types.FETCH_HOME_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          tabList: action.payload.data.head_cate_short_list,
          allTabList: action.payload.data.head_cate_all_list,
          banner : action.payload.data.home_banner,
          todayHotProducts: action.payload.data.today_hot_order_goods_list,
          page : state.page + 1,
          hotProducts: [...state.hotProducts,...action.payload.data.hot_products_list],
          officeMessage: action.payload.data.office_message,
        }
      }
    }
    // 首页分类
    case types.FETCH_CATE: {
      return {...state, fetching: true}
    }
    case types.FETCH_CATE_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          tabList: action.payload.data.head_cate_short_list,
          allTabList: action.payload.data.head_cate_all_list,
          banner : action.payload.data.home_banner,
          todayHotProducts: action.payload.data.today_hot_order_goods_list || [],
          hotProducts: action.payload.data.hot_products_list,
          childrenList: action.payload.data.children_cate_list || []
        }
      }
    }
    // 详情
    case types.CLEAR_DETAIL: {
      return {
        ...state, 
        fetching: true,
        detail: [],
        recommendGoodsList: [],
      }
    }
    case types.FETCH_DETAIL: {
      return {...state, fetching: true}
    }
    case types.FETCH_DETAIL_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          detail: action.payload.data.list,
          recommendGoodsList: action.payload.data.recommend_goods_list
        }
      }
    }
    // 转盘
    case types.FETCH_PRIZE: {
      return {...state, fetching: true}
    }
    case types.FETCH_PRIZE_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          prize: action.payload.data,
        }
      }
    }
  }
  return state;
}